from django.db import models

# Create your models here.

# 商品类别表
class Type_1(models.Model):
    id = models.AutoField(primary_key=True)
    first = models.CharField(max_length=100,verbose_name='第一类型')

    class Meta:
        db_table = 'type_1'
        verbose_name = '类别一'
        verbose_name_plural = '类别一'

    def __str__(self):
        return self.first

class Type_2(models.Model):
    id = models.AutoField(primary_key=True)
    second = models.CharField(max_length=100, verbose_name='第二类型')
    first_id = models.ForeignKey(Type_1,on_delete=models.CASCADE)

    class Meta:
        db_table = 'type_2'
        verbose_name = '类别二'
        verbose_name_plural = '类别二'

    def __str__(self):
        return self.second

# 商品状态字段
STATUS_CHOICES = (
    ('active', '在售'),
    ('inactive', '下架'),
    ('out_of_stock', '缺货'),
)
# 商品信息表
class Product(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=150,verbose_name='商品标题')
    sezes = models.CharField(max_length=100,verbose_name='商品规格')
    types = models.ForeignKey(Type_2,on_delete=models.CASCADE,verbose_name='第一类型')
    status = models.CharField(max_length=100,choices=STATUS_CHOICES,default='active',verbose_name='商品状态')
    price = models.FloatField(verbose_name='商品价格')
    price_d = models.FloatField(verbose_name='折后价格')
    likes = models.IntegerField(default=0,verbose_name='收藏数量')
    sold = models.IntegerField(default=0,verbose_name='销售数量')
    inventory= models.IntegerField(verbose_name='库存')
    create_time = models.DateField(auto_now_add=True,verbose_name='创建时间')
    update_time = models.DateField(auto_now=True,verbose_name='更新时间')
    img = models.FileField(default='imgs/加载失败.png',upload_to=r'imgs',verbose_name='商品主图')
    details = models.FileField(default='imgs/加载失败.png',upload_to=r'details',verbose_name='商品介绍图')
    describe_str = models.TextField(verbose_name='商品文字描述')

    class Meta:
        db_table = 'product'
        verbose_name = '商品'
        verbose_name_plural = '商品信息'

    def __str__(self):
        return self.name
